package com.xywang.service.provider;

import com.xywang.dao.bill.BillDao;
import com.xywang.dao.provider.ProviderDao;
import com.xywang.pojo.Provider;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


public class ProviderServiceImpl implements ProviderService{
    private BillDao billDaoImpl;
    @Autowired
    public void setBillDaoImpl(BillDao billDaoImpl) {
        this.billDaoImpl = billDaoImpl;
    }
    private ProviderDao providerDaoImpl;
    @Autowired
    public void setProviderDaoImpl(ProviderDao providerDaoImpl) {
        this.providerDaoImpl = providerDaoImpl;
    }

    @Override
    public List<Provider> getProviderListService(String proCode, String proName) {
        return providerDaoImpl.getProviderListDao(proCode,proName);
    }

    @Override
    public boolean addProService(Provider provider) {
        boolean flag = false;
        int i = providerDaoImpl.addProDao(provider);
        if(i>0){
            flag = true;
        }else{
            System.out.println("ProviderServiceImpl->addProService->错误");
        }
        return flag;
    }

    @Override
    public int deleteProviderByIdService(int delId) {
        int billCount = billDaoImpl.getBillCountByProviderId(delId);
        try{
            if (billCount == 0) {
                // 为0，表示没有任何订单和该供应商有关，可以删除，执行删除
                providerDaoImpl.deleteProviderById(delId);
            }
        }catch (Exception e){
            billCount = -1;
            System.out.println("ProviderServiceImpl->deleteProviderByIdService->error");
        }
        return billCount;
    }

    @Override
    public Provider getProviderByIdService(int id) {
        return providerDaoImpl.getProviderById(id);
    }

    @Override
    public boolean modifyService(Provider provider) {
        boolean flag = false;
        int i = providerDaoImpl.modifyDao(provider);
        if(i>0){
            flag = true;
        }else{
            System.out.println("ProviderServiceImpl->modifyService->错误");
        }
        return flag;
    }
}
